define(function(require) {
	// var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	require("cordova!com.justep.cordova.plugin.barcodescanner");// 调用为：justep.barcodeScanner；

	var Model = function() {
		this.callParent();
		this.FInfoID = "";
		this.PrdCount = 0;
	};
	Model.prototype.ReScan_ButtonClick = function(event) {
		this.QrScan();
	};
	Model.prototype.Return_ButtonClick = function(event) {
		this.FranchiserBarcodeInsert("Return_ButtonClick");
	};

	Model.prototype.FranchiserBarcodeInsert_Success = function(data, call_status) {
		console.log("FranchiserBarcodeInsert_Success()");
		var row = window.app.comp_FaHuoLieBiaoData.find([ 'FInfoID' ], [ this.FInfoID ]);
		var row2 = this.comp("ShaoMiaoData").find([ "FInfoID" ], [ this.FInfoID ]);
		row[0].val("ScanCount", row2.length);
		this.getParent().comp("Main_Contents").to("AddProducts_Content");
	};
	Model.prototype.FranchiserBarcodeInsert_err = function(err, call_status) {
		console.log("FranchiserBarcodeInsert_err()");
		console.log(err);
		console.log(call_status);
	};
	Model.prototype.ConverLargebagCode = function() {
		console.log("ConverLargebagCode()");
		var ret_string = "";
		var rows = this.comp("ShaoMiaoData").find([ "FInfoID" ], [ this.FInfoID ]);
		var ScanCode_Array = [];
		for (var i = 0; i < rows.length; i++) {
			console.log((rows[i]).val("ScanCode"));
			ScanCode_Array.push(rows[i].val("ScanCode"));
		}

		if (ScanCode_Array.length > 0) {
			ret_string = ScanCode_Array.join();
		}
		return ret_string;
	};
	Model.prototype.FranchiserBarcodeInsert = function(call_status) { // 添加一条发货明细
		console.log("FranchiserBarcodeInsert()");
		var api_url = window.app.base_url + window.app.FranchiserBarcodeInsertApi;
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		data.FID = this.FID;
		data.FInfoID = this.FInfoID;
		data.Largebagcode = this.ConverLargebagCode();
		data.CtmType = window.app.CtmType;
		console.log(data);
		var self = this;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res);

				self.FranchiserBarcodeInsert_Success(res[0], call_status);
				return;
			},
			error : function(err) {
				self.FranchiserBarcodeInsert_err(err, call_status);
				return;
			}
		});
	};

	Model.prototype.DeleteScanCode_i9Click = function(event) {
		console.log("DeleteScanCode_i9Click");
		this.comp("ShaoMiaoData").deleteData(event.bindingContext.$object);
		// this.comp("FaHuoLieBiaoData").setValue("ScanCount",
		// this.comp("FaHuoLieBiaoData").val("ScanCount") - 1);
		$(this.getElementByXid("ReScan_Button")).show();
	};
	Model.prototype.AddCodeToShaoMiaoData = function(res) {
		// TODO:只解析code=xxxxx中的数字部分
		var Code = res.text.substr(res.text.length - 16, 16);

		var row = this.comp("ShaoMiaoData").find([ 'ScanCode' ], [ Code ]);
		if (row.length !== 0) {
			console.log("数据已经存在");
			console.log(Code);
			justep.Util.hint("这个条码已经扫描过，请核对产品后再继续操作");
			return;
		}
		this.comp("ShaoMiaoData").newData({
			"index" : 0,
			"defaultValues" : [ {
				"fID" : justep.UUID.createUUID(),
				"ScanCode" : Code,
				"FInfoID" : this.FInfoID,
			} ]
		});

		var count = this.comp("ShaoMiaoData").find([ 'FInfoID' ], [ this.FInfoID ]);
		// this.comp("FaHuoLieBiaoData").setValue("ScanCount", count.length);
		console.log("继续扫描？" + this.PrdCount + ">" + count.length);
		if (this.PrdCount > count.length) {
			var that = this;
			setTimeout(function() {
				that.QrScan();
			}, 500); // 如果没有扫满，继续调用扫描
		} else if (this.PrdCount == count.length) {
			justep.Util.hint("条码扫描完毕，请核对。");
			$(this.getElementByXid("ReScan_Button")).hide();
		}
	};
	Model.prototype.QrScan_scanok = function(res) {
		window.scanstatus = "scanok";
		this.AddCodeToShaoMiaoData(res);
	};
	Model.prototype.QrScan_cancelled = function(res) {
		console.log("QrScan_cancelled()");
		window.scanstatus = "cancelled";
	};
	Model.prototype.QrScan_err = function(err) {
		console.log("QrScan_err()");
		window.scanstatus = "err";
	};
	Model.prototype.QrScan = function() {
		console.log("QrScan()");
		window.scanstatus = "scaning";
		if (justep.Browser.isX5App) {
			var self = this;
			console.log(justep.barcodeScanner);
			justep.barcodeScanner.scan(function(res) {
				var cancelled = res.cancelled;
				if (cancelled) {
					self.QrScan_cancelled(res);
				} else {
					self.QrScan_scanok(res);
				}
			}, function(ee) {
				self.QrScan_err(ee);
			});
		} else {

			var tmpstr = Math.floor(Math.random() * 10);
			this.QrScan_scanok({
				text : "http://www.315seed.com/hd/q.aspx?code=25355441320341" + tmpstr
			});
		}
	};

	Model.prototype.modelParamsReceive = function(event) {
		console.log("modelParamsRecive()");
		this.comp("ShaoMiaoData").clear();
		console.log(event.params);
		if (event.params === undefined || event.params.FInfoID === undefined) {
			justep.Util.hint("从订单号获取失败，出错了。");
		} else {
			this.FID = event.params.FID;
			this.FInfoID = event.params.FInfoID;
			this.PrdCount = event.params.PrdCount;

			// 获取已有条码
			var api_url = window.app.base_url + window.app.FranchiserBarcodeInfoApi;
			var data = {};
			data.username = window.app.username;
			data.password = window.app.password;
			data.fid = this.FID;
			data.CtmType = window.app.CtmType;
			console.log(data);
			var self = this;
			$.ajax({
				type : "POST",
				url : require.toUrl(api_url),
				data : {
					'data' : JSON.stringify(data)
				},
				dataType : 'json',
				success : function(res) {
					console.log(res);
					res.forEach(function(e) {
						self.comp("ShaoMiaoData").add({
							"fID" : justep.UUID.createUUID(),
							"ScanCode" : e.Largebagcode,
							"FInfoID" : e.FInfoID,
						});
					});

					self.comp("list2").set({
						"filter" : '$row.val("FInfoID")==' + self.FInfoID
					});
					self.comp("list2").refresh();
					var count = self.comp("ShaoMiaoData").find([ 'FInfoID' ], [ self.FInfoID ]);
					if (self.PrdCount > count.length) {
						self.QrScan();
					} else {
						$(self.getElementByXid("ReScan_Button")).hide();
					}
					return;
				},
				error : function(err) {
					return;
				}
			});
		}

	};

	Model.prototype.modelLoad = function(event) {
		console.log("modelLoad()");
	};

	Model.prototype.InputBtnClick = function(event) {
		this.comp("windowDialog1").open();
	};

	return Model;
});